package com.sdry.approval.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdry.approval.entity.BaseRequest;
import com.sdry.approval.entity.LeaveMan;
import com.sdry.approval.entity.LeaveRequest;
import com.sdry.approval.entity.PatchCardRequest;
import com.sdry.approval.service.BaseRequestService;
import com.sdry.approval.service.LeaveRequestService;
import com.sdry.common.BaseController;
import com.sdry.system.entity.SysUser;
import com.sdry.system.service.UserService;
import com.sdry.untils.UserContext;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: info_sdry
 * @description: 通用申请
 * @author: cb
 * @create: 2020-02-21 18:06
 **/
@Controller
@RequestMapping(value = "baseRequest")
public class BaseRequestController extends BaseController {

    @Autowired
    private BaseRequestService baseRequestService;

    @Autowired
    private UserService userService;

    @Autowired
    private LeaveRequestService leaveRequestService;


    /**
     * 通用申请展示
     * @return
     */
    @RequestMapping(value = "/apply")
    public String repair(Model model) {
        logger.info("通用申请展示");
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0L).eq("log_on",0L).select("user_id","real_name");
        List<SysUser> sysUserList = userService.list(queryWrapper);
        model.addAttribute("sysUserList",sysUserList);
        return "/approval/apply/base";
    }


    /**
     * 添加
     * @param baseRequest 申请对象
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public boolean add(BaseRequest baseRequest, @RequestParam("userId[]") String[] userId) {
        logger.info("补卡申请添加成功 leaveRequest = {}"+ JSONObject.fromObject(baseRequest));
        return  baseRequestService.add(baseRequest,userId);
    }


    /**
     * 查询详细
     * @return
     */
    @RequestMapping(value = "/queryBase/{requestId}")
    public String queryBase(@PathVariable("requestId") Long requestId, Model model) {
        BaseRequest baseRequest = baseRequestService.getById(requestId);
        model.addAttribute("baseRequest",baseRequest);
        return "/approval/detail/base";
    }

    /**
     * 查询详细
     * @return
     */
    @RequestMapping(value = "/queryBaseDetail/{requestId}")
    public String queryBaseDetail(@PathVariable("requestId") Long requestId, Model model) {
        BaseRequest baseRequest = baseRequestService.getById(requestId);
        List<LeaveMan> leaveMEN = leaveRequestService.queryLeaveManList(requestId, "ty");
        if (leaveMEN != null){
            for (LeaveMan leaveMAN : leaveMEN) {
                if (leaveMAN.getUserId().equals(UserContext.getSessionUserIdByServiceImpl())){
                    baseRequest.setProposal(leaveMAN.getProposal());
                }
            }
        }
        model.addAttribute("baseRequest",baseRequest);
        return "/approval/detail/alreadybase";
    }
}
